// app/controller/news.js
'use strict';


const Controller = require('egg').Controller;

// 定义创建接口的请求参数规则
const createRule = {
  topicId: 'number',
  topicType: 'number',
  content: 'string',
};

class CommentController extends Controller {
  async index() {
    const ctx = this.ctx;
    const { cms: { comment } } = ctx.service;
    const data = await comment.find(ctx.query);
    ctx.success({ data });
  }

  async show() {
    const ctx = this.ctx;
    const { cms: { comment } } = ctx.service;
    const data = await comment.findOne(ctx.params);
    ctx.success({ data });
  }

  async create() {
    const ctx = this.ctx;
    const { cms: { comment } } = ctx.service;
    ctx.validate(createRule);
    try {
      const id = await comment.create(ctx.request.body);
      const data = {
        id,
      };
      ctx.success({ data });
    } catch (error) {
      ctx.error({ detail: error });
    }
  }

  // async update() {
  //   const ctx = this.ctx;
  //   const { cms: { comment } } = ctx.service;
  //   const params = Object.assign({ id: ctx.params.id }, ctx.request.body);
  //   const data = await comment.update(params);
  //   ctx.success({ data });
  // }

  async destroy() {
    const ctx = this.ctx;
    const { cms: { comment } } = ctx.service;
    const data = await comment.destroy(ctx.params);
    ctx.success({ data });
  }
}

module.exports = CommentController;
